package net.monare.mifss.evn;

import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Notification;
import net.monare.mifss.MifssHolder;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.ApiKeyUiModule;
import net.monare.mifss.admin.ui.win.ConfigureApiKeyWindow;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.StorageServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class AddNewApiKeyListener extends BasicMifssListener implements ClickListener {

    private static final long serialVersionUID = -8827934076263270566L;

    private ApiKeyUiModule apiKeyUiModule;
    private StorageDef storageDef;
    private ConfigureStorageWindow parent;
    private ConfigureApiKeyWindow parentWindow;





    public AddNewApiKeyListener(StorageDef storageDef, ApiKeyUiModule apiKeyUiModule, ConfigureStorageWindow parent, ConfigureApiKeyWindow parentWindow) {

        this.storageDef = storageDef;
        this.apiKeyUiModule = apiKeyUiModule;
        this.parent = parent;
        this.parentWindow = parentWindow;
    }

    public void buttonClick(ClickEvent event) {

        //
        //

        try {

            ApiKey apiKey = apiKeyUiModule.getApiKey();

            if (apiKey != null) {

                getMifssHolder().getStorageService().createApiKey(storageDef.getName(), apiKey.getName(), apiKey.getDescription(),
                        apiKey.getApiMode());
                parent.resetAllPanes();
                Notification.show(MifssMessages.getMessage("sys.success.api.key.created"));
                parentWindow.close();

            } else {
                Notification.show(MifssMessages.getMessage("sys.error.api.key.not.defined"));
            }

        } catch (StorageServiceException e) {
            Notification.show(MifssMessages.getMessage("sys.error.api.key.add") + " : " + e.getMessage());
        }

    }

}
